﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml;

namespace Hp.Vertica.Client
{
    /// <summary>
    /// Interaction logic for TemplateViewer.xaml
    /// </summary>
    public partial class TemplateViewer : Window
    {
        public TemplateViewer()
        {
            InitializeComponent();

            var type = typeof(Control);
            var asm = type.Assembly;
            foreach (var t in asm.DefinedTypes.OrderBy(c => c.Name))
            {
                if (type.IsAssignableFrom(t) && !type.IsAbstract && t != type)
                {
                    try
                    {
                        var con = t.GetConstructor(new Type[] { });
                        Control obj = (Control)con.Invoke(new object[] { });
                        if (obj != null)
                        {
                            ListBoxItem item = new ListBoxItem();

                            StackPanel panel = new StackPanel();
                            item.Content = panel;

                            obj.Visibility = System.Windows.Visibility.Collapsed;
                            panel.Children.Add(obj);

                            Label lb = new Label();
                            panel.Children.Add(lb);
                            lb.Foreground = new SolidColorBrush(Colors.White);
                            lb.Content = t.Name;
                            

                            item.Selected += item_Selected;
                            lstControls.Items.Add(item);
                        }
                    }
                    catch
                    {
                        //do nothing
                    }
                }
            }
            //ListBoxItem itemForWindow = new ListBoxItem();
            //lstControls.Items.Add(itemForWindow);
            //itemForWindow.Content = "Window";
            //itemForWindow.Selected += (s, b) =>
            //{
            //    Window w = new Window();
            //    txtXamlCode.Text = this.GetTemplateXamlCode(w);
            //};

        }

        void item_Selected(object sender, RoutedEventArgs e)
        {
            try
            {
                Control obj = (Control)((StackPanel)((ListBoxItem)sender).Content).Children[0];
                txtXamlCode.Text = this.GetTemplateXamlCode(obj);
            }
            catch (Exception exc)
            {
                txtXamlCode.Text = exc.Message;
            }

        }

        private string GetTemplateXamlCode(Control ctrl)
        {

            FrameworkTemplate template = ctrl.Template;
            string xaml = "";

            if (template != null)
            {

                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.IndentChars = new string(' ', 8);
                settings.NewLineOnAttributes = true;

                StringBuilder strbuild = new StringBuilder();
                XmlWriter xmlwrite = XmlWriter.Create(strbuild, settings);

                try
                {
                    XamlWriter.Save(template, xmlwrite);
                    xaml = strbuild.ToString();
                }
                catch (Exception exc)
                {
                    xaml = exc.Message;
                }
            }
            else
            {
                xaml = "no template";
            }

            return xaml;
        }
    }
}
